/*
 * Copyright Radosław Smogura, 2012. All rights reserved.
 *
 * The content of this file is confidential and it is intelectual property
 * of copyright owners.
 * 
 *
 * $URL: http://reprap-ide.googlecode.com/svn/trunk/RepRapIDE/RepRapProject/src/main/java/eu/softper/reprap/project/RepRapProject.java $
 * $Id: RepRapProject.java 3 2012-11-19 10:58:30Z mail@smogura.eu $
 * $Revision: 3 $
 */
package eu.softper.reprap.project;

import java.awt.Image;
import java.beans.PropertyChangeListener;
import java.io.*;
import javax.swing.Icon;
import javax.swing.event.ChangeListener;
import javax.xml.bind.JAXB;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.api.project.Sources;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataLoader;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author rsmogura
 */
public class RepRapProject implements Project {
    public static final String SOURCES_DIRECTORY = "src";
    
    private FileObject projectDirectory;
    private Lookup lookups;
    private RepRapProjectConfig config;
    private Icon icon;
    private DataFolder sourcesFolder;
    
    protected RepRapProject(FileObject projectDirectory, RepRapProjectConfig config) {
        this.projectDirectory = projectDirectory;
        this.config = config;
    }

    @Override
    public FileObject getProjectDirectory() {
        return projectDirectory;
    }

    @Override
    public Lookup getLookup() {
        if (lookups == null) {
            lookups = Lookups.fixed(
                    new RepRapProjectInfo(),
                    new RepRapLogicalView(this),
                    this
                    );
        }
        
        return lookups;
    }

    public RepRapProjectConfig getConfig() {
        return config;
    }

    public DataFolder getSourcesDirectory() {
        return sourcesFolder;
    }
    
    public void addFilesToProject(File[] files, FileObject target) {
        if (target == null)
            target = getProjectDirectory();
        
        for (File f : files) {
            FileObject fo = FileUtil.toFileObject(f);
            if (target.equals(fo.getParent()))
                continue;
            try {
                fo.copy(target, fo.getName(), fo.getExt());
            }catch(IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
    }
    
    public static RepRapProject loadProject(FileObject directory) {
        FileObject configXml = directory.getFileObject(RepRapProjectFactory.REPRAP_XML);
        if (configXml == null)
            return null;
        RepRapProjectConfig config;
        try {
            config = JAXB.unmarshal(configXml.getInputStream(), RepRapProjectConfig.class);
        }catch(FileNotFoundException fnfex) {
            throw new RuntimeException(fnfex);
        }
        
        RepRapProject project = new RepRapProject(directory, config);
        FileObject sourceDir = directory.getFileObject(SOURCES_DIRECTORY);
        
        if (sourceDir == null) {
            try {
                sourceDir = directory.createFolder(SOURCES_DIRECTORY);
            }catch(IOException ioe) {
                throw new RuntimeException();
            }
        }
        
        try {
            project.sourcesFolder = (DataFolder) DataObject.find(sourceDir);
        }catch(DataObjectNotFoundException ex) {
            throw new RuntimeException(ex);
        }
        return project;
    }
    
    public static void saveProjectConfig(RepRapProjectConfig config, FileObject directory) throws IOException {
        FileObject configXml = directory.createData(
                RepRapProjectFactory.REPRAP_XML);
        try {
            OutputStream os = configXml.getOutputStream();
            JAXB.marshal(config, os);
            os.close();
        }catch(FileNotFoundException fnfex) {
            throw new RuntimeException(fnfex);
        }catch(IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }
    private class RepRapProjectInfo implements ProjectInformation {

        @Override
        public String getName() {
            return config.getName().replaceAll("\\s", "-");
        }

        @Override
        public String getDisplayName() {
            return config.getName();
        }

        @Override
        public Icon getIcon() {
            if (icon == null) {
                icon = ImageUtilities.image2Icon(ImageUtilities.loadImage("eu/softper/reprap/project/images/reprap_logo.png").getScaledInstance(16, 16, Image.SCALE_SMOOTH));
            }
            return icon;
        }

        @Override
        public Project getProject() {
            return RepRapProject.this;
        }

        @Override
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}
